package com.bw.p19.login

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.p19.R
import com.bw.p19.databinding.ActivityLoginBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {

    override fun initView() {
        //点击获取验证码 --短信（通知），10秒倒计时
        binding.codeSend.setOnClickListener {
            //不为空
            val etUsername = binding.etUsername.text.toString()
            if (etUsername.isEmpty()) {
                ToastUtils.showLong("用户名不能为空")
                return@setOnClickListener
            }
            if (!RegexUtils.isMobileExact(etUsername)){
                ToastUtils.showLong("用户名格式不正确，应该为手机号")
                return@setOnClickListener
            }
            viewModel.codeSend(etUsername)
        }
        binding.btnLogin.setOnClickListener {
            //判断非空
            val etUsername = binding.etUsername.text.toString()
            val etCode = binding.etCode.text.toString()
            if (etUsername.isEmpty()||etCode.isEmpty()) {
                ToastUtils.showLong("用户名和验证码均不能为空")
                return@setOnClickListener

            }
            if (!RegexUtils.isMobileExact(etUsername)){
                ToastUtils.showLong("用户名格式不对，应该为手机号")
                return@setOnClickListener
            }
            viewModel.loginCode(etCode,etUsername)
        }
    }

    override fun initData() {
        viewModel.codeSendSuccess.observe(this){
            if (it.code == 200){

                val manager =
                    getSystemService(NOTIFICATION_SERVICE) as NotificationManager

                if(Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
                    //高版本需要通道
                    val channel =
                        NotificationChannel("id", "name", NotificationManager.IMPORTANCE_HIGH)
                    manager.createNotificationChannel(channel)

                    val notification = Notification.Builder(this, "id")
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码：")
                        .setContentText(it.data.content.substring(8, 14))
                        .build()
                    manager.notify(0,notification)
                }else{
                    val notification = Notification.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("验证码：")
                        .setContentText(it.data.content.substring(8, 14))
                        .build()
                    manager.notify(0,notification)
                }
//                //发送通知//不同版本发送通知
//                val manager =
//                    getSystemService(NOTIFICATION_SERVICE) as NotificationManager
//                if (Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
//                    //高版本通知
//                    val notificationChannel = NotificationChannel("id", "name", NotificationManager.IMPORTANCE_DEFAULT)
//                    val notification = Notification.Builder(this)
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setContentTitle("验证码")
//                        .setContentText("登录验证码：${it.data.content.substring(8,14)}")
//                        .setChannelId("id")
//                        .build()
//                    manager.createNotificationChannel(notificationChannel)
//                    manager.notify(1,notification)
//
//                }else{
//                    //低版本
//                    val notification = Notification.Builder(this)
//                        .setSmallIcon(R.mipmap.ic_launcher)
//                        .setContentTitle("验证码")
//                        .setContentText("登录验证码：${it.data.content.substring(8,14)}")
//                        .build()
//                    manager.notify(1,notification)
//                }
                //开始倒计时
                binding.codeSend.isEnabled = false
                lifecycleScope.launch {
                    for (i in 10 downTo 1){
                        binding.codeSend.text = "${i}秒后再次获取"
                        delay(1000)
                    }
                    binding.codeSend.text = "获取验证码"
                    binding.codeSend.isEnabled = true
                }

            }
        }
        viewModel.loginCodeSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if (it.code == 200){
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("phone",it.data.phone)
                //跳转到主页面
                startActivity(Intent(this,UserActivity::class.java))
            }
        }
    }

    override fun getVideModel(): Class<LoginViewModel> = LoginViewModel::class.java

    override fun getLayoutId(): Int  = R.layout.activity_login
}